home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / std / c++ / 45 < prev    next >
Encoding:
Internet Message Format  |  1996-08-06  |  2.6 KB

  1. From: Mats.Henricson@eua.ericsson.se (Mats Henricson)
  2. Message-ID: <9601161450.AA22214@euax3i4c06.eua.ericsson.se>
  3. X-Original-Date: Tue, 16 Jan 1996 15:50:54 +0100
  4. Path: in2.uu.net!bounce-back
  5. Date: 16 Jan 96 15:27:17 GMT
  6. Approved: fjh@cs.mu.oz.au
  7. Organization: -
  8. Newsgroups: comp.std.c++
  9. Return-Path: <daemon@migs.UCAR.EDU>
  10. Subject: Re: can initialization be used with new[]?
  11. X-Auth: PGPMoose V1.1 PGP comp.std.c++
  12.     iQBFAgUBMPvD9eEDnX0m9pzZAQGsFwF/fRobrVXbB/37CffU7JbZPuZt6yAE02eu
  13.     mEp+rJii8/TNkKGuc3R12CJhfbmuH4jO
  14.     =C85d
  15.  
  16. In article <l8291j9wix9.fsf@neppari.cs.hut.fi>,
  17. "Lassi A. Tuura" <Lassi.Tuura@hut.fi> writes:
  18. >One compiler I need to use has problems with the following code:
  19. >---------------
  20. >#include <new.h>
  21. >class T {
  22. >public:
  23. >    T (int n) : _n (n) { }
  24. >    T (const T &ref) : _n (ref._n) { }
  25. >    ~T () { }
  26. >
  27. >private:
  28. >    int _n;
  29. >};
  30. >
  31. >void f ()
  32. >{
  33. >     T *ptr = new T [10] (1);  // line 14
  34. >}
  35. >---------------
  36. >
  37. >In particular, it complains about the initializer to the vector new:
  38. >  CC: "test.cc", line 14: error: initializer for array of class object
  39. >              created using `new' (1248)
  40. >
  41. >Another compiler has no problems with the code, and the one giving the
  42. >error message has not proved to be reliable.  The question is: Is this
  43. >legal C++?  I read thru 5.3.4 from the april draft and as far as I
  44. >understood the code was flagged ill-formed.  Did I interpret the draft
  45. >correctly?  If I did, how am I supposed to allocate an array of
  46. >objects of class T?  With explicit calls to operator new and placement
  47. >new?
  48.  
  49. The code is not legal. The reason is that the default constructor,
  50. which is usually T::T(), will be used when you allocate arrays of T:s.
  51. The compiler will generate a T::T() for you only in one case, namely
  52. when you yourself have explicitly delared no other constructors. In
  53. this case you have such a constructor, T::T(int n). Therefore the
  54. compiler will not make a T::T() for you, which makes it impossible to
  55. allocate arrays of T:s.
  56.  
  57. The fix is simple: Get yourself a T::T(). Another option is to give
  58. the the int parameter a default value, usually 0 works OK. This would
  59. give you this class:
  60.  
  61. class T
  62. {
  63.    public:
  64.       T (int n = 0) : _n (n) { }
  65.       T (const T &ref) : _n (ref._n) { }
  66.       ~T () { }
  67.  
  68.    private:
  69.       int _n;
  70. };
  71.  
  72. A side note: Don't use "_" as a prefix to identifiers, since that is
  73. almost illegal according to the language. It is much better to use "_"
  74. as a suffix.
  75.  
  76. Mats Henricson
  77. Stockholm
  78. Sweden
  79. ---
  80. [ comp.std.c++ is moderated.  Submission address: std-c++@ncar.ucar.edu.
  81.   Contact address: std-c++-request@ncar.ucar.edu.  The moderation policy
  82.   is summarized in http://dogbert.lbl.gov/~matt/std-c++/policy.html. ]
  83.